var currentGridURL = getRootPath()+"/role/getRolesByParam.json";
var currentrp=20;
var role_colomns = [{
    display: 'ISO',
    name: 'ids',
    width: 50,
    align: 'center'
}, {
    display: '角色名称',
    name: 'role_name',
    width: 200,
    align: 'center'
}, {
    display: '是否系统默认',
    name: 'issys',
    width: 120,
    align: 'center',
	format:function(data, item){
		switch(item){
			case 0:
                return "是";
            case 1:
                return "否";
		}
	}
}, {
    display: '角色等级',
    name: 'role_level',
    width: 80,
    align: 'center'
}, {
    display: '描述',
    name: 'memo',
    width: '400',
    align: 'center'
}];
var role_buttons = [{
    name: '添加',
    bclass: 'add',
    onpress: function(){
    }
}, {
    name: '修改',
    bclass: 'update',
    onpress: function(){
    }
}, {
    name: '删除',
    bclass: 'delete',
    onpress: function(){
    }
}, {
    name: '查询',
    bclass: 'find',
    onpress: function(){
    }
}, {
    separator: true
}];
$(document).ready(function(){
	grid = $("#role_table").flexigrid({
        dataType: 'json',
        colModel: role_colomns,
        buttons: role_buttons,
        sortname: "iso",
        sortorder: "asc",
        usepager: true,
        checkbox: true,
        nowrap: false,
        title: '角色管理',
        page: 1,
        total: 1,
        useRp: true,
        rp: currentrp,
        rpOptions: [5, 10, 15, 20, 25, 30, 40],
        resizable: false,
        width: document.documentElement.clientWidth,
        height: document.documentElement.clientHeight - 115,
        pagestat: '显示记录从{from}到{to}，总数 {total} 条',
        procmsg: '正在加载数据,请稍等...',
        nomsg: '没有符合条件的记录存在',
        errormsg: '发生错误',
        autoload: false,
        onChangePage: function(newp){
            if (!newp) {
                return false;
            }
            else {
                getGrid({
                    page: newp,
                    rp: currentrp
                }, currentGridURL);
            }
        },
        onRpChange: function(rp){
            currentrp = rp;
            getGrid({
                page: 1,
                rp: currentrp
            }, currentGridURL);
        },
        onReload: function(page){
            getGrid({
                page: page,
                rp: currentrp
            }, currentGridURL);
        }
    });
	var data = {
        page: 1,
        rp: currentrp
    };
	getGrid(data,currentGridURL);
});
function getGrid(data, url){
    $.ajax({
        type: "POST",
        url: url,
        dataType: "json",
        data: data,
        cache: false,
        async: false,
        success: function(msg){
            if (msg.failture) {
                showMsg(msg.msg, true, "错误信息", false);
            }
            else {
				grid.flexOptions({rp:data.rp});
				grid.flexAddData(msg);
            }
        }
    });
}
function getRootPath(){
    var strFullPath = window.document.location.href;
    var strPath = window.document.location.pathname;
    var pos = strFullPath.indexOf(strPath);
    var prePath = strFullPath.substring(0, pos);
    var postPath = strPath.substring(0, strPath.substr(1).indexOf('/') + 1);
    return (prePath + postPath);
}
function showMsg(msg, isbutton, title, hideicon){
    $("#menu_message").attr("title", title);
    $("#menu_message p").attr("align", "center").css("padding", "1em").addClass("ui-state-highlight ui-corner-all").html(msg);
    $("#menu_message").dialog({
        autoOpen: true,
        modal: true,
        resizable: false,
        closeOnEscape: false,
        open: function(event, ui){
            if (hideicon) {
                $("#menu_message").prev().first().find(".ui-dialog-titlebar-close").hide();
            }
        }
    });
    if (isbutton) {
        $("#menu_message").dialog("option", "buttons", [{
            text: "确定",
            click: function(){
                $(this).dialog("close");
                $(this).dialog("destroy");
            }
        }]);
    }
}